package ru.spb.osll.myfit.client.widget;

import com.google.gwt.user.client.ui.Grid;
import ru.spb.osll.myfit.common.ExecutingResult;

public class ProgressBar extends Grid implements ExecutingListener {
  private int m_elements;

  public ProgressBar() {
    this(25);
  }

  public ProgressBar(int elements) {
    super(1, elements);
    setVisible(false);

    m_elements = elements;
    setCellPadding(0);
    setCellSpacing(0);

    for(int i = 0; i < m_elements; i++) {
      Grid e = new Grid(1, 1);
      e.setStyleName("progressbarBlank");
      setWidget(0, i, e);
    }

    setWidth("100%");
    setStyleName("progressbarBorder");

  }

  public void updateView(double percentage) {
    int progressCount = (int) (m_elements * percentage);

    for (int i = 0; i < m_elements; i++) {
      Grid e = (Grid) getWidget(0, i);
      if(i < progressCount)
        e.setStyleName("progressbarFull");
      else
        e.setStyleName("progressbarBlank");
    }

  }

  @Override
  public void processExecutingResult(ExecutingResult result) {
    updateView(result.getPercentage());
  }

  @Override
  public void finish() {

  }
}

